using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

public class SPELL_Editor : EditorWindow {
    private static List<Base_Spell> spells;
	private static Vector2 scrollVector;
	private static Vector2 scrollVector2;
	private static string[] toolbarStrings  = {"Load","Save","Add", "Delete"};
	private static int selLabel=-1;
	private static SerializedProperty DamageProp1;
	
	private static GameObject Npc_Obj;
	private static GameObject Npc_Obj1;
	private static GameObject Npc_Obj2;
	private static Texture Icon;
	private static AudioClip au1;
	private static AudioClip au2;
	private static AudioClip au3;
	
	private static bool expanded =false;
	private static Spell_Type spType = Spell_Type.Damager;
	
	private static bool expanded2 =false;
	private static Spell_Apply_Type spTo = Spell_Apply_Type.Npc_Aggressive;	
	
	
	private static bool expanded3 =false;
	private static string[] _spells;
	private static int opSpell=0;
	
    [MenuItem ("Ciber Tools/Spell Editor")]
   static void Init () {
        SPELL_Editor window = (SPELL_Editor)EditorWindow.GetWindow( typeof(SPELL_Editor) );
		spells = new List<Base_Spell>();
		selLabel=-1;
		//_spells = IOClass.GetAllDerivedST(typeof(Base_Spell));
    }
    void OnGUI () {
		Show_Toolbar();
		Show_List_Spell();
		Show_Spell_Details();
    }
	
	static void Save()
	{
		//EditorUtility.DisplayDialog("Info","Save","Ok");
		string filename = EditorUtility.SaveFilePanel("Save Spell Collection",Application.dataPath+"/GData","","gdt");
		if(filename=="")
			return;
		using(var sm = File.Create(filename))
		{
			foreach(Base_Spell n in spells)
			{
				n.Save(sm);
				Debug.Log("Save - " + sm.Position.ToString() + " / " + sm.Length.ToString() );
			}
		}
	}
	static void Load()
	{
		string filename = EditorUtility.OpenFilePanel("Load Spell Collection",Application.dataPath+"/GData","gdt");
		if(filename=="")
			return;
		using(var sm = File.OpenRead(filename))
		{
			spells.Clear();
			while((sm.Position < sm.Length ))
			{
				Base_Spell n = new Base_Spell();
				n.Load(sm);
				spells.Add(n);
				Debug.Log("Load - " + sm.Position.ToString()  + " / " + sm.Length.ToString() );
			}
		}
	}
	static void AddSpell()
	{
		//EditorUtility.DisplayDialog("Info","AddNPC","Ok");
	 	Base_Spell sp = new Base_Spell();
		sp.Id = spells.Count+1;
		sp.Name = "New Spell "+sp.Id.ToString();
		sp.Description = "";
		sp.AppliedTo = Spell_Apply_Type.Npc_Aggressive;
		sp.Spell_Type = Spell_Type.Damager;
		spells.Add(sp);
	}
	static void DelSpell()
	{
		EditorUtility.DisplayDialog("Info","DelNPC","Ok");
	}
	static void Show_List_Spell(){
		
		GUILayout.BeginVertical(GUI.skin.box);
      	scrollVector = GUILayout.BeginScrollView(scrollVector);
		GUILayout.Label("Spell List", EditorStyles.boldLabel);
		if(spells !=null){
			string[] names = new string[spells.Count];
			for (int i = 0; i < spells.Count; i++)
		    {
				names[i]=spells[i].Name;
		    }
			int oldselLabel = selLabel;
			selLabel = GUILayout.SelectionGrid(selLabel,names,1);
			/*if(oldselLabel!=selLabel)
			{
				Npc_Obj = (GameObject)AssetDatabase.LoadAssetAtPath(spells[selLabel].Object_Name,typeof(GameObject));
			}*/
		}
	    GUILayout.EndScrollView();
	    GUILayout.EndVertical();
		
	}
	static void Show_Toolbar()
	{
		GUILayout.BeginVertical(GUI.skin.box);
		int pushed = GUILayout.Toolbar( -1 ,toolbarStrings);
		switch (pushed) {
			case 0: Load(); break;
			case 1: Save(); break;
			case 2: AddSpell(); break;
			case 3: DelSpell(); break;
		}
		GUILayout.EndVertical();
	}
	
	static void Show_Spell_Details()
	{
		
		int i = selLabel;
		GUILayout.BeginHorizontal(GUI.skin.box);
		GUILayout.Label("       Spell Details       ", EditorStyles.miniBoldLabel);
		if(selLabel>-1){
	      	scrollVector2 = GUILayout.BeginScrollView(scrollVector2);
				
			GUILayout.Label("ID: "); spells[i].Id = int.Parse(GUILayout.TextField(spells[i].Id.ToString()));
	        GUILayout.Label("Name: "); spells[i].Name = GUILayout.TextField(spells[i].Name);
			spells[i].Delay =  EditorGUILayout.FloatField("Cast Delay: ", spells[i].Delay);
			
			GUILayout.Label("Icon: ");
			Icon =(Texture) EditorGUILayout.ObjectField(Icon, typeof(Texture),GUILayout.Width(40),GUILayout.Height(40));
			

			
			GUILayout.Label("Description: ");	
			GUILayout.TextArea(spells[i].Description,255,GUILayout.Height(70));
			
			GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(3)); 
			SpellFlags(i);
			GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(3)); 
			
			FXObj(i);
			
			GUILayout.EndScrollView();
	    }
		GUILayout.EndHorizontal();
		
	}
	
	static void FXObj(int i)
	{

		
		GUILayout.Label("Cast FX: ");
		Npc_Obj =(GameObject) EditorGUILayout.ObjectField(Npc_Obj, typeof(GameObject));
		
		GUILayout.Label("End FX: ");
		Npc_Obj1 =(GameObject) EditorGUILayout.ObjectField(Npc_Obj1, typeof(GameObject));

		GUILayout.Label("Aff FX: ");
		Npc_Obj2 =(GameObject) EditorGUILayout.ObjectField(Npc_Obj2, typeof(GameObject));
		
		GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(3)); 
		GUILayout.Label("Cast SFX: ");
		au1 =(AudioClip) EditorGUILayout.ObjectField(au1, typeof(AudioClip));
		
		GUILayout.Label("Hit SFX: ");
		au2 =(AudioClip) EditorGUILayout.ObjectField(au2, typeof(AudioClip));
		GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(3)); 
		
		if(Icon != null)
			spells[i].patch_Icon = AssetDatabase.GetAssetPath( Icon );

		if(Npc_Obj != null)
			spells[i].patch_Src_FX = AssetDatabase.GetAssetPath( Npc_Obj );
		
		if(Npc_Obj1 != null)
			spells[i].patch_Dst_FX = AssetDatabase.GetAssetPath( Npc_Obj1 );

		if(Npc_Obj2 != null)
			spells[i].patch_Hit_FX = AssetDatabase.GetAssetPath( Npc_Obj2 );
	}
	
	static void SpellFlags(int i)
	{
		spells[i].Spell_Type = (Spell_Type)EditorGUILayout.EnumPopup("Spell Type:",spells[i].Spell_Type);
		spells[i].AppliedTo = (Spell_Apply_Type)EditorGUILayout.EnumPopup("Apply To:",spells[i].AppliedTo);
	}

}
/*
	[ProtoMember(1)]	public string patch_Icon { get; set; }
	[ProtoMember(2)]	public string patch_Src_FX { get; set; }
	[ProtoMember(3)]	public string patch_Dst_FX { get; set; }
	[ProtoMember(4)]	public float Delay { get; set; }
	[ProtoMember(5)]	public string Name { get; set; }
	[ProtoMember(6)]	public string Description { get; set; }
	[ProtoMember(7)]	public Spell_Type Spell_Type { get; set; }
	[ProtoMember(8)]	public Spell_Apply_Type AppliedTo { get; set; }
	[ProtoMember(9)]	public float Speed { get; set; }
	[ProtoMember(10)]	public string patch_Sound_Cast { get; set; }
	[ProtoMember(11)]	public string patch_Sound_Hit { get; set; }
	[ProtoMember(12)]	public string patch_Hit_FX { get; set; }	
	[ProtoMember(13)]	public int Id { get; set; }	
*/